﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RM.Common;
using System.Data;
using System.Data.SqlClient;
using RM.DBUtility;
namespace RM.DAL
{
    public partial class RMMenu
    {
        public RMMenu()
        { }
        /// <summary>
        /// 根据ID判断数据是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,modulename,title,navigateurl,sortindex,parentmenuid,remark");
            strSql.Append(" from X_Menu");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters ={
                                          new SqlParameter("@id",SqlDbType.Int)
                                      };
            parameters[0].Value = id;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 通过标题判断数据是否存在
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public bool Exists(string title)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,modulename,title,navigateurl,imageurl,sortindex,parentmenuid,remark");
            strSql.Append(" from X_Menu");
            strSql.Append(" where title=@title");
            SqlParameter[] parameters = {
                                            new SqlParameter("@title",SqlDbType.VarChar,200)
                                        };
            parameters[0].Value = title;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Model.RMMenu GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,modulename,title,navigateurl,imageurl,sortindex,parentmenuid,remark");
            strSql.Append(" from X_Menu");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters ={
                                          new SqlParameter("@id",SqlDbType.Int)
                                      };
            parameters[0].Value = id;
            Model.RMMenu model = new Model.RMMenu();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sortindex"].ToString() != "")
                {
                    model.SortIndex = int.Parse(ds.Tables[0].Rows[0]["sortindex"].ToString());
                }
                if (ds.Tables[0].Rows[0]["parentmenuid"].ToString() != "")
                {
                    model.ParentMenuId = int.Parse(ds.Tables[0].Rows[0]["parentmenuid"].ToString());
                }
                model.ModuleName = ds.Tables[0].Rows[0]["modulename"].ToString();
                model.Title = ds.Tables[0].Rows[0]["title"].ToString();
                model.NavigateUrl = ds.Tables[0].Rows[0]["navigateurl"].ToString();
                model.ImageUrl = ds.Tables[0].Rows[0]["imageurl"].ToString();
                model.Remark = ds.Tables[0].Rows[0]["remark"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Model.RMMenu model)
        {
            using (SqlConnection Conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                Conn.Open();
                using (SqlTransaction trans = Conn.BeginTransaction())
                {
                    try
                    {
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("insert into X_Menu(");
                        strSql.Append("modulename,title,navigateurl,imageurl,sortindex,parentmenuid,remark)");
                        strSql.Append(" values (");
                        strSql.Append("@modulename,@title,@navigateurl,@imageurl,@sortindex,@parentmenuid,@remark)");
                        strSql.Append(";select @@IDENTITY");
                        SqlParameter[] parameters ={
                                                      new SqlParameter("@modulename",SqlDbType.VarChar,200),
                                                      new SqlParameter("@title",SqlDbType.VarChar,200),
                                                      new SqlParameter("@navigateurl",SqlDbType.VarChar,200),
                                                      new SqlParameter("@imageurl",SqlDbType.VarChar,200),
                                                      new SqlParameter("@sortindex",SqlDbType.Int),
                                                      new SqlParameter("@parentmenuid",SqlDbType.Int),
                                                      new SqlParameter("@remark",SqlDbType.VarChar,200)
                                                  };
                        object obj = DbHelperSQL.GetSingle(Conn, trans, strSql.ToString(), parameters);
                        model.Id = Convert.ToInt32(obj);//得到刚插入的新ID

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return 0;
                    }
                }
            }
            return model.Id;
        }
        /// <summary>
        /// 修改一列数据
        /// </summary>
        public bool UpdateField(string title, string strValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update X_Menu set " + strValue);
            strSql.Append(" where title=" + title);
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool Update(Model.RMMenu model)
        {
            using (SqlConnection Conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                Conn.Open();
                using (SqlTransaction trans = Conn.BeginTransaction())
                {
                    try
                    {
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("update X_Menu set ");
                        strSql.Append("modulename=@modulename,");
                        strSql.Append("title=@title,");
                        strSql.Append("navigateurl=@navigateurl,");
                        strSql.Append("imageurl=@imageurl,");
                        strSql.Append("sortindex=@sortindex,");
                        strSql.Append("parentmenuid=@parentmenuid,");
                        strSql.Append("remark=@remark");
                        strSql.Append(" where id=@id");
                        SqlParameter[] parameters ={
                                                      new SqlParameter("@modulename",SqlDbType.VarChar,200),
                                                      new SqlParameter("@title",SqlDbType.VarChar,200),
                                                      new SqlParameter("@navigateurl",SqlDbType.VarChar,200),
                                                      new SqlParameter("@imageurl",SqlDbType.VarChar,200),
                                                      new SqlParameter("@sortindex",SqlDbType.Int),
                                                      new SqlParameter("@parentmenuid",SqlDbType.Int),
                                                      new SqlParameter("@remark",SqlDbType.VarChar,200),
                                                      new SqlParameter("@id",SqlDbType.Int,4)};
                        parameters[0].Value = model.ModuleName;
                        parameters[1].Value = model.Title;
                        parameters[2].Value = model.NavigateUrl;
                        parameters[3].Value = model.ImageUrl;
                        parameters[4].Value = model.SortIndex;
                        parameters[5].Value = model.ParentMenuId;
                        parameters[6].Value = model.Remark;
                        parameters[7].Value = model.Id;
                        DbHelperSQL.ExecuteSql(Conn, trans, strSql.ToString(), parameters);
                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from X_Menu ");
            strSql.Append(" where id=@id");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
